<?php
/* 	File name 	: zetro_helper.php
	Author		: Iswan Putera S.Kom
	Version		: 2.0
	Created Date: 18-02-2013
*/

//===========begin helper====================
	
	
	function tglFromSql($data,$long=false)
	{
		$yy=explode('-',$data);
		return ($long==false)?substr($yy[2],0,2).'/'.$yy[1].'/'.$yy[0]:
				substr($yy[2],0,2).'/'.$yy[1].'/'.$yy[0].' '.substr($yy[2],2,10);
	}
	function dropdownBln($select='')
	{
		$bln="";//<option value=''></option>";
		for($i=1;$i<=12;$i++)
		{
			$slct=($i==$select)?"selected='selected'":'';
			$bln.="<option value='".$i."' ".$slct.">".nBulan($i)."</option>";
		}

		return $bln;
	}
	
	function tglToSql($data,$delimiter='/')
	{
		$dd=explode($delimiter,$data);
		return $dd[2].$dd[1].$dd[0];
	}
	function longDate($tgl=''){
	 ($tgl=='')? $tanggal=date('d F Y'):
	 $tanggal=substr($tgl,0,2)." ". nBulan(round(substr($tgl,3,2),0))." ". substr($tgl,6,4);
	 	return $tanggal;
	}
	function nBulan($bln,$short=false)
	{
		$bulan=($short==false)?
		array('','January','February','March','April','May','June','July','August','September','October','November','December'):
		array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Augs','Sept','Oct','Nov','Dec');
		return $bulan[(int)$bln];
	}
	function nHari($hari)
	{
		$nHari=array('Minggu','Senin','Selasa','Rabu','Kamis','Jum\'at','Sabtu');
		return $nHari[(int)$hari];	
	}
	function blnRomawi($bln)
	{
		$bulan=array('','I','II','III','IV','V','VI','VII','VIII','IX','X','XII','XII');
		return $bulan[(int)$bln];	
	}
	/* 
	create link of css
	*/
	function link_css($filename,$path='asset/css/')
	{
		if(is_array($filename)){
			foreach ($filename as $nfile){
				echo "<link href='".base_url().$path.$nfile.".css' rel='stylesheet'>";
			}
		}else{
				echo "<link href='".base_url().$path.$filename.".css' rel='stylesheet'>";
		}
	}
	/*
	crete link of javascript
	*/
	function link_js($filename,$path='asset/js/')
	{
		if(is_array($filename)){
			foreach ($filename as $nfile){
				echo "<script type='text/javascript' src='".base_url().$path.$nfile.".js'></script>";
			}
		}else{
				echo "<script type='text/javascript' src='".base_url().$path.$filename.".js'></script>";
		}
	}
	 /*
		link for jquery Calender click
	 */
	function Calender()
	{
		link_css('calendar-win2k-cold-2','asset/calender/css/');
		link_js('jquery.dynDateTime','asset/calender/');
		link_js('calendar-en.min','asset/calender/lang/');
	}
	function CalenderUI()
	{
		link_css('jquery-ui_v1.10.1','asset/calender/');
		link_js('jquery-ui_v1.10.1','asset/calender/');	
	}
	/**
	 create list with div and span
	 using css bootstrap
	*/
	function div($content='',$class='',$id='')
	{
		$data="<div class='$class' id='$id' style='border:1px solid #000'>".$content."</div>";	
		return $data;
	}
	function span($content='',$class='',$id='')
	{
		$data="<span class='$class' id='$id'>".$content."</span>";
		return $data;
	}
	/*
	create table function for less typing when generate tabel
	with default css bootstrap
	*/
	
	function tabel($class='table table-hover',$width='100%',$id='NewTable')
	{
		return "<table class='$class' id='$id' name='$id' width='$width' style='border-collapse:collapse'>";	
	}
	function _tabel()
	{
		return "</table>";	
	}
	function tr($class='',$th=false,$align='',$valign='middle')
	{
		if($th==true)
		{
			return "<thead><tr class='$class' align='center' valign='$valign'>";	
		}else{
			return "<tr class='$class' align='$align' valign='$valign'>";
		}
	}
	function _tr($th=false)
	{
		return ($th==true)?"</tr></thead><tbody>":"</tr>";
	}
	
	function tContent($end=false)
	{
		return ($end==false)?"<tbody>":"</tbody>";
	}
	
	function td($content='',$width='',$th=false,$class='',$align='',$colspan='')
	{
		if($th==true)
		{
			$alg=($align=='')?'center':$align;
		 	return "<th width='$width' class='$class' align='".$alg."' $colspan>$content</th>";	
		}else{
			return "<td class='$class' align='$align' $colspan>$content</td>";	
		}
	}
	
	function in_progress()
	{
		
	}
	function success($data)
	{
		return (!$data)?
				"<i class='cus-tick'></i> <small>Data berhasil di simpan</small>":
				"<i class='cus-error-add'></i><small> ".mysql_error()."</small>";	
	}
	//generate data set to view
	function _generate_view_mdl($header='',$menus='',$section='',$modal='',$frm_file='zetro_master')
	{
		$sec=explode(",",$section);
		$data=array();
		//menu header
		$data['tab_header']=$this->zetro_form->generate_tab_header($header);
		//generate form and new lokasi
		$this->zetro_form->frm_file('asset/frm/'.$frm_file.'.frm');
		$this->zetro_auth->menus($menus);
		$this->zetro_form->section($sec[0]);
		$this->zetro_form->get_select_opt();
		$this->zetro_form->buttons(($this->zetro_auth->cek_oto('c')!='')?true:false);
		$data['modal']=$this->zetro_form->dialog($this->zetro_form->frm_std('frm2'),$modal);
		//generate list data lokasi
		$content='';//$this->get_list_lokasi();
		$this->zetro_form->section($sec[1]);
		$this->zetro_form->action_col(true);
		$content.="";//$this->zetro_form->DataNotFound($lst_usr,4);
		$this->zetro_form->tabel_content($content);
		$data['list']=$this->zetro_form->list_std();
		//send data to view
		return $data;
	}
	
	function rdb($tabel,$where='',$field='')
	{
		$CI =& get_instance();
		$CI->load->model('main_model');
			$d='';$lst_usr=array();
			$CI->table_name($tabel);
			$CI->query();
			$CI->where($where);
			$CI->set_methods('select');
			$lst_usr=$CI->set_data();
			if($field!=''){
				foreach($lst_usr as $r)
				{
					$d=$r->$field;	
				}
				return $d;
			}else{
				return $lst_usr->result();	
			}
	}
	
 if ( ! function_exists('force_ssl'))
{
    function force_ssl()
    {
        $CI =& get_instance();
        $CI->config->config['base_url'] = str_replace('http://', 'https://', $CI->config->config['base_url']);
        if ($_SERVER['SERVER_PORT'] != 443)
        {
           // redirect($CI->uri->uri_string());
        }
    }
}  
//end of zetro helper
//localion : application/helpers